/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */
package com.shishuo.cms.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.shishuo.cms.constant.FolderConstant;
import com.shishuo.cms.entity.Folder;
import com.shishuo.cms.entity.vo.ArticleVo;
import com.shishuo.cms.entity.vo.FolderVo;
import com.shishuo.cms.exception.FolderNotFoundException;

/**
 * @author Herbert
 * 
 */
@Controller
@RequestMapping("/folder")
public class FolderAction extends BaseAction {
	private final static int ROWS=5;//每页显示数量
	/**
	 * 目录ID
	 * 
	 * @param ename
	 * @param pageNum
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/{folderId}.htm", method = RequestMethod.GET)
	public String folder(@PathVariable long folderId,
			@RequestParam(value = "p", defaultValue = "1") long p,
			@RequestParam(value ="page", defaultValue = "1") int page,
			ModelMap modelMap) {
		try {
			Folder folder = folderService.getFolderById(folderId);
//			List<ArticleVo> articles = fileService.getArticleListByFolderId(folderId);//该目录下的所有文章
//			modelMap.put("articles", articles);
//			List<Folder> parentsFolders=findAllParentsFolder(folder.getPath());//该目录的所有父级目录对象集合，用于显示“当前位置”
			FolderVo fatherFolder = folderService.getFolderById(folderService.firstFolderId(folderId));//父目录
			List<FolderVo> childrenFolders=folderService.getFolderListByFatherId(Long.valueOf(folder.getPath().split("#")[0]),FolderConstant.status.display);//得到所有子目录
//			 modelMap.put("parentsFolders", parentsFolders);
			 modelMap.put("childrenFolders", childrenFolders);
			modelMap.put("folder", folder);
			modelMap.put("g_folderId", fatherFolder.getFolderId());
			modelMap.put("g_folderObj", fatherFolder);
//			modelMap.put("g_folderObj", fatherFolder);//加入folder对象
			modelMap.put("p", p);
			
//			//分页处理
//			if(folder.getName().contains("招") || folder.getName().contains("产品") || folder.getName().contains("新闻"))//目前只有这三个功能需要分页，写死。
//			{
////				int page=0;//用户请求访问第几页 默认每页5行数据
//				int totalNum=fileService.getArticleCountByFolderId(folderId);//获得总数
//				int pages=totalNum/ROWS +((totalNum%ROWS)==0?0:1);//计算总页数
//				if(page<1) page=1;
//				if(page>pages) page=pages;
//				int offset=(page-1)*ROWS;//偏移量
//				List<ArticleVo> articles=fileService.getArticleListByFolderId(folderId, offset, ROWS);
//				modelMap.put("articles", articles);
//				modelMap.put("page", page);//当前页码
//				modelMap.put("pages", pages);//总页数
//			}
//			else//不需分页的目录，查询全部文章
//			{
//				List<ArticleVo> articles = fileService.getArticleListByFolderId(folderId);//该目录下的所有文章
//				modelMap.put("articles", articles);
//			}
			
			
			
			return themeService.getFolderTemplate(folder.getFolderId());
		} catch (Exception e) {
			modelMap.addAttribute("g_folderId", 0);
			logger.fatal(e.getMessage());
			return themeService.get404();
		}
	}
	
//	@RequestMapping(value = "/{folderId}.htm", method = RequestMethod.GET)
//	public String folder(@PathVariable long folderId){
//		try {
//			
//		} catch (Exception e) {
//			// TODO: handle exception
//		}
//	}
	/**   
	* 描述：   通过给定path查询所有祖先目录
	* 创建人：YangJ  邮箱:yangjie@sxw100.com   
	* 创建时间：2015年6月8日 下午2:24:06   
	* 修改备注：   
	* @version    
	*    
	*/ 
	private List<Folder> findAllParentsFolder(String path ){
		String[] folderIds=path.split("#");
		List<Folder> list=new ArrayList<Folder>();
		if(folderIds.length>1)
		for(int i=0;i<folderIds.length-1;i++)
			try {
				list.add(folderService.getFolderById(Long.valueOf(folderIds[i])));
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (FolderNotFoundException e) {
				e.printStackTrace();
			}
		return list;
	}
	
}
